# -*- coding: utf-8 -*-

import http.client
import json

from net.utils import utf8_decode


def basic_usage():
    conn = http.client.HTTPConnection(host="127.0.0.1", port=8080)
    conn.request("GET", "/fivechess/")
    r1 = conn.getresponse()
    print(r1.status, r1.reason)
    print(utf8_decode(r1.read()))


def post_json(host='localhost', port=8080, url='/', json_str='{}'):
    conn = http.client.HTTPConnection(host=host, port=port)
    headers = {'Content-type': 'application/json'}
    conn.request('POST', url, json_str, headers)
    response = conn.getresponse()
    return utf8_decode(response.read())


if __name__ == '__main__':
    basic_usage()
    _dict = {'session_id': 'abc123456'}
    json_content = json.dumps(_dict)
    resp = post_json(url='/fivechess/join', json_str=json_content)
    print(resp)
